package daily.oct;

public class lc3354 {
    /*
    从评论区得到启发，思路为：判断每个0左右两边数字和，差1则结果加一，相同则结果加二。
     */
    public int countValidSelections(int[] nums) {
        int n = nums.length;
        int res = 0;

        // 遍历每个位置，如果是0就检查
        for (int i = 0; i < n; i++) {
            if (nums[i] == 0) {
                int leftSum = 0;
                int rightSum = 0;

                // 计算0左边的和
                for (int j = 0; j < i; j++) {
                    leftSum += nums[j];
                }

                // 计算0右边的和
                for (int j = i + 1; j < n; j++) {
                    rightSum += nums[j];
                }

                int diff = Math.abs(leftSum - rightSum);
                if (diff == 0) {
                    res += 2;
                } else if (diff == 1) {
                    res += 1;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        lc3354 lc = new lc3354();
        int[] nums1 = {1,0,2,0,3};
        int[] nums2 = {2,3,4,0,4,1,0};
        System.out.println(lc.countValidSelections(nums1));
        System.out.println(lc.countValidSelections(nums2));
    }
}
